package com.sright.bcard.mini.web.controller;

import com.sright.bcard.common.share.service.WxappService;
import com.sright.bcard.content.share.request.CountButtonParam;
import com.sright.bcard.content.share.request.EnterpriseCardAddParam;
import com.sright.bcard.content.share.service.*;
import com.sright.bcard.enterprise.share.service.AddressService;
import com.sright.bcard.enterprise.share.service.CompanyService;
import com.sright.bcard.enterprise.share.service.EnterpriseService;
import com.sright.bcard.mini.web.constant.WebUrl;
import com.sright.bcard.user.share.request.TokenParam;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 企业名片controller
 */
@RestController
public class EnterpriseCardController {
    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private CardService cardService;

    @Autowired
    private WxappService wxappService;

    @Autowired
    private DataService dataService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AddressService addressService;

    /**
     * 人气页数据（原来基础增加返回参数）
     * @return
     */
    @GetMapping(WebUrl.DataResult)
    public SrightResponse dataResult(@RequestAttribute TokenParam tokenParam) {
        return dataService.dataResult(tokenParam.getEnterpriseUserId());
    }

    /**
     * 获取企业信息
     * @return
     */
    @GetMapping(WebUrl.EnterpriseInfo)
    public SrightResponse enterpriseInfo(@RequestAttribute TokenParam tokenParam) {
        return enterpriseService.enterpriseInfo(tokenParam.getEnterpriseId());
    }

    /**
     * 获取企业成员列表
     * @return
     */
    @GetMapping(WebUrl.EnterpriseMember)
    public SrightResponse enterpriseMember(@RequestParam(value = "page") Integer page,
                                           @RequestParam(value = "pageSize") Integer pageSize,
                                           @RequestParam(value = "keywords", required = false) String keywords,
                                           @RequestAttribute TokenParam tokenParam) {
        return enterpriseService.enterpriseMember(page, pageSize, keywords, tokenParam.getEnterpriseId());
    }

    /**
     * 添加企业名片
     * @return
     */
    @PostMapping(WebUrl.EnterpriseCardUser)
    public SrightResponse addCardEnterprise(@RequestBody EnterpriseCardAddParam enterpriseCardAddParam,@RequestAttribute TokenParam tokenParam) {
        enterpriseCardAddParam.setMemberId(tokenParam.getMemberId());
        return cardService.addCardEnterprise(enterpriseCardAddParam);
    }

    /**
     * 跳转点击次数
     * @param param
     * @param tokenParam
     * @return
     */
    @PostMapping(WebUrl.EnterpriseCountJumps)
    public SrightResponse countJumpsButton(@RequestBody CountButtonParam param, @RequestAttribute TokenParam tokenParam,HttpServletRequest request) {
        buildButtonContext(param,tokenParam);
        param.setType(3);
        return dataService.countButton(param,request);
    }

    /**
     * 企业访问次数
     * @param param
     * @param tokenParam
     * @return
     */
    @PostMapping(WebUrl.CountEnterpriseVisits)
    public SrightResponse countEnterpriseVisits(@RequestBody CountButtonParam param, @RequestAttribute TokenParam tokenParam,HttpServletRequest request) {
        buildButtonContext(param,tokenParam);
        param.setType(2);
        return dataService.countButton(param,request);
    }

    /**
     * 修改企业名片中文信息
     * @param enterpriseCardAddParam
     * @param tokenParam
     * @return
     */
    @PutMapping(WebUrl.EditEnterpriseCardZh)
    public SrightResponse editEnterpriseCardZh(@RequestBody EnterpriseCardAddParam enterpriseCardAddParam,@RequestAttribute TokenParam tokenParam) {
        enterpriseCardAddParam.setMemberId(tokenParam.getMemberId());
        enterpriseCardAddParam.setEnterpriseId(tokenParam.getEnterpriseId());
        enterpriseCardAddParam.setUserId(tokenParam.getEnterpriseUserId());
        return cardService.editCardEnterpriseZh(enterpriseCardAddParam);
    }

    /**
     * 修改企业名片英文信息
     * @param enterpriseCardAddParam
     * @param tokenParam
     * @return
     */
    @PutMapping(WebUrl.EditEnterpriseCardEn)
    public SrightResponse editEnterpriseCardEn(@RequestBody EnterpriseCardAddParam enterpriseCardAddParam,@RequestAttribute TokenParam tokenParam) {
        enterpriseCardAddParam.setMemberId(tokenParam.getMemberId());
        enterpriseCardAddParam.setEnterpriseId(tokenParam.getEnterpriseId());
        return cardService.editCardEnterpriseEn(enterpriseCardAddParam);
    }

    /**
     * 获取企业微信小程序信息
     * @param tokenParam
     * @return
     */
    @GetMapping(WebUrl.EnterpriseWxapp)
    public SrightResponse getEnterpriseWxappInfo(@RequestAttribute TokenParam tokenParam) {
        return wxappService.getEnterpriseWxappInfo(tokenParam.getEnterpriseId());
    }


    @GetMapping(WebUrl.CompanyList)
    public SrightResponse companyList(Long enterpriseId) {
        return companyService.getList(enterpriseId);
    }

    @GetMapping(WebUrl.AddressList)
    public SrightResponse addressList(Long enterpriseId) {
        return addressService.getList(enterpriseId);
    }

    private EnterpriseCardAddParam buildCardUserContext(EnterpriseCardAddParam enterpriseCardAddParam, TokenParam tokenParam) {
        enterpriseCardAddParam.setMemberId(tokenParam.getMemberId());
        enterpriseCardAddParam.setUserId(tokenParam.getEnterpriseUserId());
        enterpriseCardAddParam.setEnterpriseId(tokenParam.getEnterpriseId());
        return enterpriseCardAddParam;
    }

    private CountButtonParam buildButtonContext(CountButtonParam param, TokenParam tokenParam) {
        param.setMemberId(tokenParam.getMemberId());
        param.setUserId(tokenParam.getEnterpriseUserId());
        param.setEnterpriseId(tokenParam.getEnterpriseId());
        return param;
    }



}
